package com.castle.fortress.admin.goods.service.impl;

import cn.hutool.core.util.StrUtil;
import com.castle.fortress.admin.utils.BizCommonUtil;
import com.castle.fortress.common.utils.ConvertUtil;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.castle.fortress.admin.goods.entity.GoodsImgEntity;
import com.castle.fortress.admin.goods.dto.GoodsImgDto;
import com.castle.fortress.admin.goods.mapper.GoodsImgMapper;
import com.castle.fortress.admin.goods.service.GoodsImgService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import java.util.List;

/**
 * 商品图片表 服务实现类
 *
 * @author majunjie
 * @since 2021-11-24
 */
@Service
public class GoodsImgServiceImpl extends ServiceImpl<GoodsImgMapper, GoodsImgEntity> implements GoodsImgService {
    /**
    * 获取查询条件
    * @param goodsImgDto
    * @return
    */
    public QueryWrapper<GoodsImgEntity> getWrapper(GoodsImgDto goodsImgDto){
        QueryWrapper<GoodsImgEntity> wrapper= new QueryWrapper();
        if(goodsImgDto != null){
            GoodsImgEntity goodsImgEntity = ConvertUtil.transformObj(goodsImgDto,GoodsImgEntity.class);
            wrapper.like(goodsImgEntity.getId() != null,"id",goodsImgEntity.getId());
            wrapper.like(goodsImgEntity.getGoodsId() != null,"goods_id",goodsImgEntity.getGoodsId());
            wrapper.like(StrUtil.isNotEmpty(goodsImgEntity.getImg()),"img",goodsImgEntity.getImg());
            wrapper.like(StrUtil.isNotEmpty(goodsImgEntity.getSort()),"sort",goodsImgEntity.getSort());
            wrapper.like(goodsImgEntity.getCreateUser() != null,"create_user",goodsImgEntity.getCreateUser());
            wrapper.like(goodsImgEntity.getCreateTime() != null,"create_time",goodsImgEntity.getCreateTime());
            wrapper.like(goodsImgEntity.getUpdateUser() != null,"update_user",goodsImgEntity.getUpdateUser());
            wrapper.like(goodsImgEntity.getUpdateTime() != null,"update_time",goodsImgEntity.getUpdateTime());
            wrapper.like(goodsImgEntity.getStatus() != null,"status",goodsImgEntity.getStatus());
            wrapper.like(goodsImgEntity.getIsDeleted() != null,"is_deleted",goodsImgEntity.getIsDeleted());
            wrapper.orderByDesc("create_time");
        }
        return wrapper;
    }


    @Override
    public IPage<GoodsImgDto> pageGoodsImg(Page<GoodsImgDto> page, GoodsImgDto goodsImgDto) {
        QueryWrapper<GoodsImgEntity> wrapper = getWrapper(goodsImgDto);
        Page<GoodsImgEntity> pageEntity = new Page(page.getCurrent(), page.getSize());
        Page<GoodsImgEntity> goodsImgPage=baseMapper.selectPage(pageEntity,wrapper);
        Page<GoodsImgDto> pageDto = new Page(goodsImgPage.getCurrent(), goodsImgPage.getSize(),goodsImgPage.getTotal());
        pageDto.setRecords(ConvertUtil.transformObjList(goodsImgPage.getRecords(),GoodsImgDto.class));
        return pageDto;
    }

    @Override
    public IPage<GoodsImgDto> pageGoodsImgExtends(Page<GoodsImgDto> page, GoodsImgDto goodsImgDto){
        Map<String,Long> pageMap = BizCommonUtil.getPageParam(page);
        GoodsImgEntity goodsImgEntity = ConvertUtil.transformObj(goodsImgDto,GoodsImgEntity.class);
        List<GoodsImgEntity> goodsImgList=baseMapper.extendsList(pageMap,goodsImgEntity);
        Long total = baseMapper.extendsCount(goodsImgEntity);
        Page<GoodsImgDto> pageDto = new Page(page.getCurrent(), page.getSize(),total);
        pageDto.setRecords(ConvertUtil.transformObjList(goodsImgList,GoodsImgDto.class));
        return pageDto;
    }
    @Override
    public GoodsImgDto getByIdExtends(Long id){
        GoodsImgEntity  goodsImgEntity = baseMapper.getByIdExtends(id);
        return ConvertUtil.transformObj(goodsImgEntity,GoodsImgDto.class);
    }

    @Override
    public List<GoodsImgDto> listGoodsImg(GoodsImgDto goodsImgDto){
        QueryWrapper<GoodsImgEntity> wrapper = getWrapper(goodsImgDto);
        List<GoodsImgEntity> list = baseMapper.selectList(wrapper);
        return ConvertUtil.transformObjList(list,GoodsImgDto.class);
    }

    @Override
    public void deleteByGoodsId(Long goodsId) {
        if(goodsId == null){
            return;
        }
        baseMapper.deleteByGroupId(goodsId);
    }
}

